/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.espier.t9keyboard;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;


import android.content.Context;
import android.content.res.Resources;


/**
 * Utility methods for processing dates.
 */
public class CallLogDateUtils {
    public static final TimeZone UTC_TIMEZONE = TimeZone.getTimeZone("UTC");

    // All the SimpleDateFormats in this class use the UTC timezone
    public static final SimpleDateFormat NO_YEAR_DATE_FORMAT =
            new SimpleDateFormat("MM-dd", Locale.US);
    public static final SimpleDateFormat FULL_DATE_FORMAT =
            new SimpleDateFormat("yyyy-MM-dd", Locale.US);
    public static final SimpleDateFormat DATE_AND_TIME_FORMAT =
            new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
    public static final SimpleDateFormat NO_YEAR_DATE_AND_TIME_FORMAT =
            new SimpleDateFormat("--MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);

    // Variations of ISO 8601 date format.  Do not change the order - it does affect the
    // result in ambiguous cases.
    private static final SimpleDateFormat[] DATE_FORMATS = {
        FULL_DATE_FORMAT,
        DATE_AND_TIME_FORMAT,
        new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'", Locale.US),
        new SimpleDateFormat("yyyyMMdd", Locale.US),
        new SimpleDateFormat("yyyyMMdd'T'HHmmssSSS'Z'", Locale.US),
        new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'", Locale.US),
        new SimpleDateFormat("yyyyMMdd'T'HHmm'Z'", Locale.US),
    };

    private static final java.text.DateFormat FORMAT_WITHOUT_YEAR_MONTH_FIRST =
            new SimpleDateFormat("MMMM dd");

    private static final java.text.DateFormat FORMAT_WITHOUT_YEAR_DATE_FIRST =
            new SimpleDateFormat("dd MMMM");

    static {
        for (SimpleDateFormat format : DATE_FORMATS) {
            format.setLenient(true);
            format.setTimeZone(UTC_TIMEZONE);
        }
        NO_YEAR_DATE_FORMAT.setTimeZone(UTC_TIMEZONE);
        FORMAT_WITHOUT_YEAR_MONTH_FIRST.setTimeZone(UTC_TIMEZONE);
        FORMAT_WITHOUT_YEAR_DATE_FIRST.setTimeZone(UTC_TIMEZONE);
    }

  
    
	/**
	 * return date String Such as  07-13  or today
	 * @param long TimeInMillis
	 * @return  return date String Such as 07-13  or today
	 */
	public static String getDateString(Context context,long timeInMillis){
		Calendar calendar=Calendar.getInstance();
		int nowDateOfYear=calendar.get(Calendar.DAY_OF_YEAR);
		
		calendar.setTimeInMillis(timeInMillis);
		int dateOfYear=calendar.get(Calendar.DAY_OF_YEAR);
		if(dateOfYear-nowDateOfYear==1){
			return context.getResources().getString(R.string.calllog_tomorrow);
		}else if(dateOfYear==nowDateOfYear){
			return context.getResources().getString(R.string.calllog_today);
		}
		
		//SimpleDateFormat simpleDateFormat=new SimpleDateFormat("MM月dd日");
		//return simpleDateFormat.format(calendar.getTime());
		return NO_YEAR_DATE_FORMAT.format(calendar.getTime());
	}
	
	public static String getDurationTimeString(Context context,int seconds){
		Resources rs=context.getResources();
		StringBuilder sb=new StringBuilder();
		if(seconds>=60*60){
			sb.append(seconds/(60*60)+" ");
			sb.append(rs.getString(R.string.calllog_hour)+" ");
			seconds=seconds%(60*60);
		}
		if(seconds>=60){
			sb.append(seconds/60+" ");
			seconds=seconds%60;
		}else{
			sb.append("00 ");
		}
		sb.append(rs.getString(R.string.calllog_minites)+" ");
		sb.append(seconds+" ");
		//sb.append(rs.getString(R.string.calllog_second));
		
		return sb.toString();
	}
	
	
}
